/*  File Name	: ChatMonitor.h
 *  Author		: Javie Pong
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-10-4
 *  Description:
 */

#ifndef CHATMONITOR_H_
#define CHATMONITOR_H_
#include "Monitor.h"
#include "ChatService.h"
#include "Configurator.h"
#include "MonitorSock.h"

struct ChatPlayer
{
	ChatPlayer(int p=0)
	{
		reset();
	}
	void reset()
	{
		roleid=0;
		svcid=0;
		room = 0;
		name="";
		table=0;
		svc = NULL;
	}
	void set();
	int roleid;
	int svcid;
	int room;
	int table;
	ChatService *svc;
	std::string name;
};

class ChatMonitor:public Monitor<ChatService>
{
public:
	typedef MonitorAcceptor<ChatMonitor, ChatService> ChatAcceptor;
	typedef ObjectPool<ChatPlayer> PlayerPool;
	typedef std::map<int, ChatPlayer*> PlayerMap;
	typedef std::map<int, PlayerMap> TableMap;
private:
	typedef Monitor<ChatService> super;

public:
	virtual ~ChatMonitor();

	static ChatMonitor* instance(void);

	virtual int init(int line_id,int list_size=10240,int block_size=4096,int pool_size=10240,
			int growth_size=1024,std::string pool_name="ChatMonitor-Msg");

	virtual int start(void);

	virtual int stop(void);

	virtual int run_request_loop(void);

	virtual int run_respond_loop(void);

	ChatPlayer *find_player(int roleid);

	int bind_player(int roleid,ChatPlayer *player);

	int unbind_player(int roleid);

	int bind_table(ChatPlayer *player);

	int unbind_table(ChatPlayer *player);

	int login(int svcid,MessageBlock *mb);

	int logout(int roleid);

	int chat(int roleid,MessageBlock *mb);

	int broadcast_to_table(int table,MessageBlock *mb);

	const Json::Value& cfg(void);

	void collection_info(void);
private:
	ChatMonitor();

	int process_request(MessageBlock *mb);

	int process_respond(MessageBlock *mb);

	static ChatMonitor* instance_;

	ChatAcceptor *acceptor_;
	int line_id_;

	PlayerPool player_pool_;
	PlayerMap player_map_;
	TableMap table_map_;

	RT_MUTEX rt_mutex_;
};

#endif /* CHATMONITOR_H_ */
